//
//  DownloadViewController.m
//  MXClassroom
//
//  Created by 你个LB on 16/9/8.
//  Copyright © 2016年 你个LB. All rights reserved.
//

#import "DownloadViewController.h"

@interface DownloadViewController ()

@end

@implementation DownloadViewController

- (void)dealloc
{
    [self removeObserver:self forKeyPath:@"scrollView.contentOffset" context:nil];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    // 创建储存空间试图
    [self initMemoryView];
    
    // 创建选择按钮视图
    _segmented = [[CXSegmentedBarView alloc] initWithFrame:CGRectMake(0, 0, kScreen_width, 45)];
    _segmented.backgroundColor = [UIColor clearColor];
    _segmented.titleColor = kColor_text_color;
    _segmented.selectedTitleColor = [UIColor blackColor];
    _segmented.selectedViewbgColor = [UIColor blackColor];
    _segmented.titles = @[@"已下载",@"下载中"];
    
    // 添加事件
    [_segmented addTarget:self action:@selector(segmentedValueChanged:) forControlEvents:UIControlEventValueChanged];
    [self.navigationController.navigationBar addSubview:_segmented];
    // 3.设置视图的内容视图
    // 01 设置滑动内容视图的高度
    self.contentSizeHeight = kScreen_height - 64 - 40;
    // 02 设置滑动视图的位置
    self.scrollView.top = 64 + 40;
    // 03 设置滑动视图内容视图
    DownloadedViewController * downloaded = [[DownloadedViewController alloc] init];
    
    DownloadingViewController * downloading = [[DownloadingViewController alloc] init];
    
    self.viewControllers = @[downloaded, downloading];
    
    // 4.添加观察者模式
    [self addObserver:self forKeyPath:@"scrollView.contentOffset" options:NSKeyValueObservingOptionNew context:nil];
}

// 创建储存空间试图
- (void)initMemoryView{
    MemoryView * memoryView = [[MemoryView alloc] initWithFrame:CGRectMake(0, 64, kScreen_width, 40)];
    [self.view addSubview:memoryView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 选项按钮事件
- (void)segmentedValueChanged:(CXSegmentedBarView *)segmented
{
    self.selectedIndex = segmented.selectedIndex;
}

#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context
{
    
    if ([keyPath isEqualToString:@"scrollView.contentOffset"]) {
        // 1.获取所监听到的内容
        id value = change[@"new"];
        // 2.获取当前滑动视图水平方向的偏移量
        CGFloat scrollView_contentOffset_x = [value CGPointValue].x;
        // 3.获取当前滑动视图的页数索引
        NSInteger pageIndex = (int)scrollView_contentOffset_x / (int)kScreen_width;
        // 获取当前页面视图的偏移宽度
        float p_width = scrollView_contentOffset_x - pageIndex * kScreen_width;
        // 4.获取当前页面的偏移比例
        float scale = p_width / kScreen_width;
        // 5.设置选中视图的位置
        _segmented.selectedView.left = pageIndex * _segmented.selectedView.width + _segmented.selectedView.width * scale;
    }
}

#pragma mark - 监听当前选中页面的索引位置
- (void)setSelectedIndex:(NSInteger)selectedIndex
{
    [super setSelectedIndex:selectedIndex];
    
    // 设置选项按钮选中的索引位置
    _segmented.selectedIndex = self.selectedIndex;
}


@end
